package com.crm.controller;
import com.crm.base.ResponseTest;
import com.crm.domain.dto.DictDto;
import com.crm.domain.entity.DictData;
import com.crm.domain.vo.DataVO;
import com.crm.domain.vo.DictTypeVO;
import com.crm.domain.vo.OneVO;
import com.crm.domain.vo.RowsVO;
import com.crm.mapper.DictDataMapper;
import com.crm.service.DictDataService;
import com.crm.service.DictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 15890
 */
@RestController
@RequestMapping("/system/dict")
@RequiredArgsConstructor
public class DictController {
    private final DictDataService dictDataService;
    private final DictTypeService dictTypeService;
    private final ServerProperties serverProperties;
    private final DictDataMapper dictDataMapper;

    /**
     * 字典类型的分页查询
     * @param pageNum
     * @param pageSize
     * @param dictName
     * @param dictType
     * @param status
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/type/list")
    public DictTypeVO list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "dictName", required = false) String dictName,
            @RequestParam(value = "dictType", required = false) String dictType,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "params[beginTime]", required = false) String beginTime,
            @RequestParam(value = "params[endTime]", required = false) String endTime) {
        DictTypeVO vo = dictTypeService.list(pageNum,pageSize,dictName,dictType,status,beginTime,endTime);
        return vo;
    }

    /**
     * 字典类型、下来菜单的接口
     * @param dictType
     * @return
     */
    @GetMapping("/data/type/{dictType}")
    public DataVO getSysNormalDisable(@PathVariable String dictType) {
        // 你可以在这里使用 dictType 值
        List<DictData> list = dictDataService.selectAll(dictType);
        DataVO vo = new DataVO();
        vo.setMsg("操作成功");
        vo.setData(list);
        vo.setCode(200);
        return vo;
    }

    /**
     * 新增字典类型
     * @param dictDto
     */
    @PostMapping("/type")
    public ResponseTest addDictType(@RequestBody DictDto dictDto) {
        ResponseTest responseTest = dictTypeService.add(dictDto);
        return responseTest;
    }

    /**
     * 删除字典类型
     * @param dictId
     * @return
     */
    @DeleteMapping("/type/{dictId}")
    public ResponseTest delDictType(@PathVariable List<Long> dictId) {
        dictTypeService.deleteAll(dictId);
        return new ResponseTest("操作成功",200);
    }

    /**
     * 字典类型根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/type/{id}")
    public OneVO selectById(@PathVariable Long id) {
        OneVO vo = dictTypeService.select(id);
        return vo;
    }

    /**
     * 修改字典类型
     * @param dictDto
     * @return
     */
    @PutMapping("/type")
    public OneVO updatgeDictType(@RequestBody DictDto dictDto) {
        OneVO vo = dictTypeService.updateOne(dictDto);

        return vo;
    }


    /**
     * 字典数据分页查询
     * @param pageNum
     * @param pageSize
     * @param dictType
     * @param status
     * @param dictLabel
     * @return
     */
    @GetMapping("/data/list")
    public RowsVO listByPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "dictType",required = false) String dictType,
                             @PathVariable(value = "status",required = false) Integer status,
                             @PathVariable(value = "dictLabel",required = false) String dictLabel) {
        RowsVO vo = dictDataService.listByPage(pageNum,pageSize,dictType,status,dictLabel);
        return vo;
    }

    /**
     * 新增字典数据
     */
    @PostMapping("/data")
    public OneVO addDictData(@RequestBody DictDto dictDto){
        OneVO vo= dictDataService.addDictData(dictDto);
        return vo;
    }

    /**
     * 删除字典数据
     * @param id
     * @return
     */
    @DeleteMapping("/data/{id}")
    public OneVO deleteDictData(@PathVariable Long id){
        dictDataMapper.deleteById(id);
        OneVO<DictData> vo = new OneVO<>();
        vo.setMsg("操作成功");
        vo.setCode(200);
        return vo;
    }

    /**
     * 修改字典数据
     * @return
     */
    @PutMapping("/data")
    public OneVO updateData(@RequestBody DictData dictData){
        dictDataMapper.updateById(dictData);
        OneVO<DictData> vo = new OneVO<>();
        vo.setCode(200);
        vo.setMsg("操作成功");
        return vo;
    }

    @GetMapping("/data/{id}")
    public OneVO selectDataById(@PathVariable Long id){
        DictData dictData = dictDataMapper.selectById(id);
        OneVO<DictData> vo = new OneVO<>();
        vo.setMsg("查询成功");
        vo.setData(dictData);
        vo.setCode(200);
        return vo;
    }

}